package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomLogin;
import br.com.telescope.adsrt.dom.DomTextLong;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.InsertFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.QueryFeature;
import br.com.telescope.t2.feature.SearchFeature;
import br.com.telescope.t2.feature.UpdateFeature;
import br.com.telescope.util.ObjectUtil;
import solutions.ferpa.ztest.ent.*;

/**
 * Preferências de usuários.
 * <p>
 * Interface de uso restrito aos administradores do sistema que permite 
 * manter as preferências de qualquer usuário.
 * <P>
 * Telescope origin: RTPREF.CAD_PREFERENCIAS_USUARIOS
 * <P>
 * Base entity: RTPREF.PREFERENCIAS_USUARIOS
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadPreferenciasUsuarios extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTPREF.CAD_PREFERENCIAS_USUARIOS";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Preferências de usuários");
    }
    
    protected EntPreferenciasUsuarios prus;
    protected EntPreferencias pref;
    protected EntUsuarios usua;
    protected EntPreferenciasUsuarios rec; // NEED_TJW1_COMPATIBILITY
    
    // Record items
    private DomTextLong aValor;
    private DomId aPreferenciaId;
    private DomId aId;
    private DomId aUsuarioId;
    private DomUpper aPreferencia;
    private DomTextLong aValorPadrao;
    private DomTextLong aDescricao;
    private DomLogin aLogin;
    
    // Query items
    private DomUpper qPreferencia;
    private DomLogin qLogin;
    private DomTextLong qValorPadrao;
    private DomTextLong qValor;
    
    @Override
    public void copyDefaultValues() {
        copyDefaultValue(prus.getUsuarioId(), aUsuarioId);
    }
    
    @Override
    public String getBaseSql() {
        return "select PRUS.*"
                + " , PREF.PREFERENCIA as PREFERENCIA"
                + " , PREF.VALOR as VALOR_PADRAO"
                + " , PREF.DESCRICAO as DESCRICAO"
                + " , USUA.LOGIN as LOGIN"
                + " , PREF.ID as Z_PREF_ID"
                + " , USUA.ID as Z_USUA_ID"
                + " from PREFERENCIAS_USUARIOS PRUS"
                + " join PREFERENCIAS PREF on PREF.ID = PRUS.PREFERENCIA_ID"
                + " join USUARIOS USUA on USUA.ID = PRUS.USUARIO_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aValor = new DomTextLong();
        aValor.context(ctx).name("VALOR").label("Valor");
        
        aPreferenciaId = new DomId();
        aPreferenciaId.context(ctx).name("PREFERENCIA_ID").label("Preferência");
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
        aUsuarioId = new DomId();
        aUsuarioId.context(ctx).name("USUARIO_ID").label("Usuário");
        aUsuarioId.setDefaultValue(""
        + ctx.getSessionString("USER_ID") + "");
        
        aPreferencia = new DomUpper();
        aPreferencia.context(ctx).name("PREFERENCIA").label("Preferência").width(60);
        aPreferencia.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT PREFERENCIA as valor"
                + " , PREFERENCIA as pesquisar"
                + " , PREFERENCIA as descricao"
                + " from PREFERENCIAS PREF"
                + " where pref.por_usuario='S')", "q"));
        aPreferencia.setAutocompleteLength(2);
        
        aValorPadrao = new DomTextLong();
        aValorPadrao.context(ctx).name("VALOR_PADRAO").label("Valor").width(60);
        aValorPadrao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT VALOR as valor"
                + " , VALOR as pesquisar"
                + " , VALOR as descricao"
                + " from PREFERENCIAS PREF"
                + " where pref.por_usuario='S')", "q"));
        aValorPadrao.setAutocompleteLength(2);
        
        aDescricao = new DomTextLong();
        aDescricao.context(ctx).name("DESCRICAO").label("Descrição").width(60).height(2);
        aDescricao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from PREFERENCIAS PREF"
                + " where pref.por_usuario='S')", "q"));
        aDescricao.setAutocompleteLength(2);
        aDescricao.setDefaultValue("Inserido automaticamente!");
        
        aLogin = new DomLogin();
        aLogin.context(ctx).name("LOGIN").label("Login");
        aLogin.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT LOGIN as valor"
                + " , LOGIN as pesquisar"
                + " , LOGIN as descricao"
                + " from USUARIOS USUA)", "q"));
        aLogin.setAutocompleteLength(1);
        
        
        qPreferencia = new DomUpper();
        qPreferencia.context(ctx).name("PREFERENCIA").label("Preferência");
        qPreferencia.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT PREFERENCIA as valor"
                + " , PREFERENCIA as pesquisar"
                + " , PREFERENCIA as descricao"
                + " from PREFERENCIAS PREF"
                + " where pref.por_usuario='S')", "q"));
        qPreferencia.setAutocompleteLength(2);
        
        qLogin = new DomLogin();
        qLogin.context(ctx).name("LOGIN").label("Login");
        qLogin.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT LOGIN as valor"
                + " , LOGIN as pesquisar"
                + " , LOGIN as descricao"
                + " from USUARIOS USUA)", "q"));
        qLogin.setAutocompleteLength(1);
        
        qValorPadrao = new DomTextLong();
        qValorPadrao.context(ctx).name("VALOR_PADRAO").label("Valor");
        qValorPadrao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT VALOR as valor"
                + " , VALOR as pesquisar"
                + " , VALOR as descricao"
                + " from PREFERENCIAS PREF"
                + " where pref.por_usuario='S')", "q"));
        qValorPadrao.setAutocompleteLength(2);
        
        qValor = new DomTextLong();
        qValor.context(ctx).name("VALOR").label("Valor");
    }
    
    @Override
    public String getQueryPkColumn() {
        return "PRUS.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntPreferenciasUsuarios getRecord() {
        if (prus == null) {
            prus = new EntPreferenciasUsuarios(ctx);
            rec = prus;
        }
        return this.prus;
    }
    
    @Override
    public String getContextName() {
        return aPreferencia.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "PREFERENCIA, LOGIN, VALOR";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // PREFERENCIA_ID
        try {
            if (form.hasFields("PREFERENCIA")) {
                query = createQuery()
                        .select("PREF.ID")
                        .from("PREFERENCIAS", "PREF")
                        .where("pref.por_usuario='S'")
                        .filter("PREF.PREFERENCIA", form.get("PREFERENCIA"))
                        ;
                prus.setPreferenciaId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
        
        // USUARIO_ID
        try {
            if (form.hasFields("LOGIN")) {
                query = createQuery()
                        .select("USUA.ID")
                        .from("USUARIOS", "USUA")
                        .filter("USUA.LOGIN", form.get("LOGIN"))
                        ;
                prus.setUsuarioId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    @Override
    public void translateUpdateFks(Form form) {
        ModelQuery query;
        
        // PREFERENCIA_ID
        try {
            if (form.hasFields("PREFERENCIA")) {
                query = createQuery()
                        .select("PREF.ID")
                        .from("PREFERENCIAS", "PREF")
                        .where("pref.por_usuario='S'")
                        .filter("PREF.PREFERENCIA", form.get("PREFERENCIA"))
                        ;
                prus.setPreferenciaId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
        
        // USUARIO_ID
        try {
            if (form.hasFields("LOGIN")) {
                query = createQuery()
                        .select("USUA.ID")
                        .from("USUARIOS", "USUA")
                        .filter("USUA.LOGIN", form.get("LOGIN"))
                        ;
                prus.setUsuarioId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="QUERY">
    public class FQuery extends QueryFeature {
        
        @Override
        public void initForm() {
            addField(qPreferencia, "opt");
            addField(qLogin, "opt");
            addField(qValor, "opt");
        }
        
        {
            setFeatureName("QUERY");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aPreferencia, "view");
            addField(aLogin, "view");
            addField(aValor, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends InsertFeature {
        
        @Override
        public void initForm() {
            aPreferencia.setOptional(false);
            addField(aPreferencia, "rqd");
            aLogin.setOptional(false);
            addField(aLogin, "rqd");
            aValor.setOptional(false);
            addField(aValor, "rqd");
            aValorPadrao.setEditable(false);
            addField(aValorPadrao, "view");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE">
    public class FUpdate extends UpdateFeature {
        
        @Override
        public void initForm() {
            aPreferencia.setOptional(false);
            addField(aPreferencia, "rqd");
            aLogin.setOptional(false);
            addField(aLogin, "rqd");
            aValor.setOptional(false);
            addField(aValor, "rqd");
            aValorPadrao.setEditable(false);
            addField(aValorPadrao, "view");
        }
        
        {
            setFeatureName("UPDATE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aPreferencia, "view");
            addField(aLogin, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="SEARCH">
    public class FSearch extends SearchFeature {
        
        @Override
        public void initForm() {
            addField(qPreferencia, "search");
            addField(qLogin, "search");
            addField(qValor, "search");
            addField(qValorPadrao, "search");
        }
        
        {
            setFeatureName("SEARCH");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="PREFERENCIA_LOOKUP">
    public class FPreferenciaLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aPreferencia.setEditable(true);
            addField(aPreferencia, "opt");
            aValorPadrao.setEditable(false);
            addField(aValorPadrao, "view");
            aDescricao.setEditable(false);
            addField(aDescricao, "view");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("PREF.PREFERENCIA as PREFERENCIA")
                    .select("PREF.VALOR as VALOR_PADRAO")
                    .select("PREF.DESCRICAO as DESCRICAO")
                    .from("PREFERENCIAS", "PREF")
                    .where("pref.por_usuario='S'")
                    ;
        }
        
        {
            setFeatureName("PREFERENCIA_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LOGIN_LOOKUP">
    public class FLoginLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aLogin.setEditable(true);
            addField(aLogin, "opt");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("USUA.LOGIN as LOGIN")
                    .from("USUARIOS", "USUA")
                    ;
        }
        
        {
            setFeatureName("LOGIN_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FQuery());
        addFeature(new FList());
        addFeature(new FInsert());
        addFeature(new FUpdate());
        addFeature(new FDelete());
        addFeature(new FSearch());
        addFeature(new FPreferenciaLookup());
        addFeature(new FLoginLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "QUERY";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getValor() {
        return aValor.getValue();
    }
    
    public void setValor(String value) {
        this.aValor.setValue(value);
    }
    
    public Long getPreferenciaId() {
        return aPreferenciaId.getValue();
    }
    
    public void setPreferenciaId(Long value) {
        this.aPreferenciaId.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public Long getUsuarioId() {
        return aUsuarioId.getValue();
    }
    
    public void setUsuarioId(Long value) {
        this.aUsuarioId.setValue(value);
    }
    
    public String getPreferencia() {
        return aPreferencia.getValue();
    }
    
    public void setPreferencia(String value) {
        this.aPreferencia.setValue(value);
    }
    
    public String getValorPadrao() {
        return aValorPadrao.getValue();
    }
    
    public void setValorPadrao(String value) {
        this.aValorPadrao.setValue(value);
    }
    
    public String getDescricao() {
        return aDescricao.getValue();
    }
    
    public void setDescricao(String value) {
        this.aDescricao.setValue(value);
    }
    
    public String getLogin() {
        return aLogin.getValue();
    }
    
    public void setLogin(String value) {
        this.aLogin.setValue(value);
    }
    public String getQueryPreferencia() {
        return qPreferencia.getQueryValue();
    }
    
    public void setQueryPreferencia(String value) {
        this.qPreferencia.setQueryValue(value);
    }
    public String getQueryLogin() {
        return qLogin.getQueryValue();
    }
    
    public void setQueryLogin(String value) {
        this.qLogin.setQueryValue(value);
    }
    public String getQueryValor() {
        return qValor.getQueryValue();
    }
    
    public void setQueryValor(String value) {
        this.qValor.setQueryValue(value);
    }
    // </editor-fold>
    
    
    @Override
    public String getJsServiceFile() {
        return "ui/rtpref/cad_preferencias_usuarios.js";
    }
}
